/**
 * 长按功能封装
 * 使用方法:将需要长按的区域包裹着本组件内
 * <longTouch bind:longTouch="longTouch" bind:shortTouch="shortTouch">
 *  <view class="touch-area">...</view>
 * </longTouch>
 */

var timerOut = null;
Component({

  /**
   * 组件的初始数据
   */
  properties: {

  },
  data: {
    myEventDetail: {},
    touch_start: '',
    touch_end: ''
  },
  detached: function () {
    clearTimeout(timerOut);
  },
  methods: {
    myTouchStart(e) {
      let myEventDetail = {};
      var that = this;
      timerOut = null;
      this.data.touch_start = '';
      this.data.touch_end = '';
      this.setData({
        touch_start: e.timeStamp
      });
      timerOut = setTimeout(function () {
        that.triggerEvent('longTouch', myEventDetail, {
          bubbles: false
        });
      }, 1000)
    },
    myTouchEnd(e) {
      this.setData({
        touch_end: e.timeStamp
      });
      clearTimeout(timerOut);
    },
    tapEl(e) {
      let touchTime = this.data.touch_end - this.data.touch_start;
      let myEventDetail = {};
      clearTimeout(timerOut);
      if (touchTime < 1000) {
        this.triggerEvent('shortTouch', myEventDetail, {
          bubbles: false
        });
      }
    }
  }
})